<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>奥枫运维目录配置</title>
    <link href="/img/icon.ico" rel="shortcut icon"/>
    <link rel="stylesheet" href="/layui/css/layui.css"  media="all">
    <script src="/layui/layui.js" charset="utf-8"></script>
    <script src="/js/jquery.min.js" charset="utf-8"></script>
    <script src="/js/Treatment.js" charset="utf-8"></script>
</head>

<body id="body">
<div class="layui-col-md12">
    <blockquote class="layui-elem-quote layui-text" style="margin-top: 15px;">
        <a  class="layui-btn layui-btn-primary layui-border-blue" href="/">返回主页</a>
    </blockquote>
</div>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>添加配置信息(应用名称不可重复)</legend>
</fieldset>
<div class="layui-btn-container" style="text-align:center">
    <button type="button" class="layui-btn" onclick="toSaveConfig()">保存配置</button>
    <button type="button" class="layui-btn" onclick="toLoadConfig()">加载配置</button>
</div>
<div class="layui-btn-container" style="text-align:center">
    <button type="button" class="layui-btn layui-btn-warm" onclick="addOther(null)">添加其他</button>
    <button type="button" class="layui-btn layui-btn-normal" onclick="addTomcat(null)">添加Tomcat</button>
    <button type="button" class="layui-btn layui-btn-warm" onclick="addNginx(null)">添加Nginx</button>
</div>

<div class="layui-row">
    <div class="layui-col-xs12">
        <fieldset class="layui-elem-field layui-field-box" style="margin-top: 20px;" id="OtherBox">
            <legend>其他配置项(应用名称不可重复)</legend>
        </fieldset>
    </div>
</div>



<div class="layui-row">
    <div class="layui-col-xs6">
        <form class="layui-form" action="" id="TomcatForm">
            <fieldset class="layui-elem-field layui-field-box" style="margin-top: 20px;" id="TomcatBox">
                <legend>Tomcat配置项(应用名称不可重复)</legend>
            </fieldset>
        </form>
    </div>
    <div class="layui-col-xs6">
        <form class="layui-form" action="" id="NginxForm">
            <fieldset class="layui-elem-field layui-field-box" style="margin-top: 20px;" id="NginxBox">
                <legend>Nginx配置项(应用名称不可重复)</legend>
            </fieldset>
        </form>
    </div>
</div>



</body>
<script>
    let inner = sessionStorage.getItem('isPassAdopt')
    if (inner !== 'success'){
        console.log('in')
        $('#body').html(`违法请求，请重新验证：<a href="/">验证</a>`)
    };



    function addTomcat(one) {
        $('#TomcatBox').append(`
                <fieldset class="layui-elem-field layui-field-box one-config" style="margin-top: 20px;">
                    <legend>
                    <div class="layui-inline layui-btn-container">
                        <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="deleteThis(this)"><i class="layui-icon"></i></button>
                    </div>
                    </legend>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">应用名称</label>
                            <div class="layui-input-inline">
                                <input type="tel" class="af_name layui-input" value="${one === null ? '' : one.af_name}"  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">文件路径</label>
                            <div class="layui-input-inline">
                                <input type="text" class="af_path layui-input" value="${one === null ? '' : one.af_path}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">端口</label>
                            <div class="layui-input-inline">
                                <input type="number" class="af_port layui-input" value="${one === null ? '' : one.af_port}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </fieldset>`)
        if (one === null){
            $(window).scrollTop($('#TomcatBox fieldset').eq(-1).offset().top);
        }
    }
    function addNginx(one) {
        $('#NginxBox').append(`
                <fieldset class="layui-elem-field layui-field-box one-config" style="margin-top: 20px;">
                    <legend>
                        <div class="layui-inline layui-btn-container">
                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="deleteThis(this)"><i class="layui-icon"></i></button>
                        </div>
                    </legend>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">应用名称</label>
                            <div class="layui-input-inline">
                                <input type="tel" class="af_name layui-input" value="${one === null ? '' : one.af_name}"  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">文件路径</label>
                            <div class="layui-input-inline">
                                <input type="text" class="af_path layui-input" value="${one === null ? '' : one.af_path}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">端口</label>
                            <div class="layui-input-inline">
                                <input type="number" class="af_port layui-input" value="${one === null ? '' : one.af_port}"  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">启动文件</label>
                            <div class="layui-input-inline">
                                <input type="text" class="af_file layui-input" value="${one === null ? '' : one.af_file}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </fieldset>
        `)
        if (one === null){
            $(window).scrollTop($('#NginxBox fieldset').eq(-1).offset().top);
        }
    }

    function addOther(one) {
        $('#OtherBox').append(`
                <div class="layui-col-xs4 one-config">
                <fieldset class="layui-elem-field layui-field-box" style="margin-top: 20px;">
                    <legend>
                        <div class="layui-inline layui-btn-container">
                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="deleteThis(this)"><i class="layui-icon"></i></button>
                        </div>
                    </legend>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">应用名称</label>
                            <div class="layui-input-inline">
                                <input type="tel" class="af_name layui-input" value="${one === null ? '' : one.af_name}"  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">文件路径</label>
                            <div class="layui-input-inline">
                                <input type="text" class="af_path layui-input" value="${one === null ? '' : one.af_path}" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </fieldset>
            </div>
        `)
        if (one === null){
            $(window).scrollTop($('#OtherBox fieldset').eq(-1).offset().top);
        }
    }

    /**
     * 保存配置信息
     */
    function toSaveConfig() {
        let innerConfig = {
            tomcat: induceTomcat(),
            nginx: induceNginx(),
            other: induceOther()
        }
        console.log(innerConfig)
        asyncPost('/saveDirConfig',innerConfig,Loading('正在保存'),function (res) {
            success('保存成功')
        })
    }

    /**
     * 删除按钮所在的配置项
     * @param doc
     */
    function deleteThis(doc) {
        $(doc).parents('.one-config').eq(0).remove()
    }

    /**
     * 获取Tomcat配置信息
     */
    function induceTomcat() {
        let tomcats = $('#TomcatBox').children('.one-config')
        let tomcatJson = []
        for (let i = 0; i < tomcats.length; i++) {
            let one = $(tomcats[i])
            if (!(one.find('.af_name').val() && one.find('.af_path').val() && one.find('.af_port').val())){
                warning('Tomcat配置第'+(i+1)+'项存在未填写项 跳过未填写配置')
                continue;
            }
            tomcatJson[i] = {
                af_name:one.find('.af_name').val().trim(),
                af_path:one.find('.af_path').val().trim(),
                af_port:one.find('.af_port').val().trim()
            }
        }
        return tomcatJson;
    }
    /**
     * 获取nginx配置信息
     */
    function induceNginx() {
        let nginxs = $('#NginxBox').children('.one-config')
        let nginxJson = []
        for (let i = 0; i < nginxs.length; i++) {
            let one = $(nginxs[i])
            if (!(one.find('.af_name').val() && one.find('.af_path').val() && one.find('.af_port').val() && one.find('.af_file').val())){
                warning('nginx配置第'+(i+1)+'项存在未填写项 跳过未填写配置')
                continue;
            }
            nginxJson[i] = {
                af_name:one.find('.af_name').val().trim(),
                af_path:one.find('.af_path').val().trim(),
                af_port:one.find('.af_port').val().trim(),
                af_file:one.find('.af_file').val().trim()
            }
        }
        return nginxJson;
    }
    /**
     * 获取其他配置信息
     */
    function induceOther() {
        let others = $('#OtherBox').children('.one-config')
        let othersJson = []
        for (let i = 0; i < others.length; i++) {
            let one = $(others[i])
            if (!(one.find('.af_name').val() && one.find('.af_path').val())){
                warning('other配置第'+(i+1)+'项存在未填写项 跳过未填写配置')
                continue;
            }
            othersJson[i] = {
                af_name:one.find('.af_name').val().trim(),
                af_path:one.find('.af_path').val().trim()
            }
        }
        return othersJson;
    }

    /**
     * 根据已有配置获取可视化页面
     */
    function toLoadConfig(){
        asyncGet('/getDirConfig',null,Loading('正在加载配置'),function (res) {
            $('#TomcatBox').html(`<legend>Tomcat配置项(应用名称不可重复)</legend>`)
            $('#NginxBox').html(`<legend>Nginx配置项(应用名称不可重复)</legend>`)
            $('#OtherBox').html(`<legend>其他配置项(应用名称不可重复)</legend>`)
            let data = res.data
            if (data.tomcat){
                for (let i = 0; i < data.tomcat.length; i++) {
                    addTomcat(data.tomcat[i])
                }
            }
            if (data.nginx){
                for (let i = 0; i < data.nginx.length; i++) {
                    addNginx(data.nginx[i])
                }
            }
            if (data.other){
                for (let i = 0; i < data.other.length; i++) {
                    addOther(data.other[i])
                }
            }
            layui.use(['form'], function(){
                layui.form.render()
            });
            success('加载成功');
        })
    }

    toLoadConfig()

    layui.use('util', function(){
        let util = layui.util;

        //执行
        util.fixbar({
        });
    });
</script>
</html>